home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-16 | 14.3 KB | 549 lines | [TEXT/MPS ] |
- /*
- File: event.c
-
- Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- //
- // You may incorporate this sample code into your applications
- // without restriction. This sample code has been provided "AS
- // IS" and the responsibility for its operation is 100% yours.
- // You are not permitted to redistribute the source as "Apple
- // sample code" after having made changes. If you're going to
- // re-distribute the source, we require that you make it clear
- // in the source that the code was descended from Apple sample
- // code, but that you've made changes.
- //
-
- #pragma segment AppSeg
-
- #ifndef Common_Defs
- #include "Common.h"
- #endif
-
- extern Document* gFrontDocument;
- extern short gQuit, gQuitting;
- extern short gInBackground;
- extern Boolean gCanDrag;
-
- void DoContent(Document* theDocument, EventRecord* theEvent);
- void DoBackgroundContent(Document* theDocument, EventRecord* theEvent);
- void DoMouseDown(EventRecord* theEvent);
- void DoKey(char theChar);
- void DoKeyDown(EventRecord* theEvent);
- void DoActivate(EventRecord* theEvent);
- void DoUpdate(EventRecord* theEvent);
- void DoOSEvent(EventRecord* theEvent);
- void DoHighLevelEvent(EventRecord* theEvent);
- void DoEvent(EventRecord* theEvent);
- void DoIdle(void);
-
- extern ControlActionUPP myActionProcUPP; // for Mixed Mode
-
- #define SleepDuration 20 // WaitNextEvent() sleep constant
-
- // *****************************************************************************
- // *
- // * DoContent()
- // *
- // * Handles mouseDown events in the content region of a document window.
- // *
- // * (1) If the mouseDown is on a control, handle the click by calling TrackControl.
- // *
- // * (2) If the mouseDown is on a draggable object (the document's hiliteRgn) and a
- // * successful drag occurs, no further processing is necessary.
- // *
- // * (3) If the mouseDown is on a draggable object and the mouse is released without
- // * dragging, set the insertion point to the original mouseDown location by calling
- // * TEClick with the mouseDown information.
- // *
- // * (4) If the mouseDown is not on a draggable object and within the viewRect of the
- // * TextEdit field, call TEClick to handle the mouseDown.
- // *
- // *****************************************************************************
- void DoContent(Document* theDocument, EventRecord* theEvent)
- {
- short thePart;
- Point thePoint;
- ControlHandle theControl;
-
- if (theDocument->theTE != NULL)
- {
- SetPort(theDocument->theWindow);
-
- thePoint = theEvent->where;
- GlobalToLocal(&thePoint);
-
- if (thePart = FindControl(thePoint,(WindowRef)theDocument->theWindow,&theControl))
- {
- if (theControl == theDocument->vScroll)
- {
- if (thePart == kControlIndicatorPart)
- {
- TrackControl(theControl,thePoint,0L);
- AdjustDocumentView(theDocument);
- }
- else
- TrackControl(theControl,thePoint,myActionProcUPP);
-
- AdjustScrollBar(theDocument);
- }
- }
- else
- if (PtInRgn(thePoint,theDocument->hiliteRgn))
- {
- if (gCanDrag)
- if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
- TEClick(thePoint,false,theDocument->theTE);
- }
- else
- if (PtInRect(thePoint,&(**(theDocument->theTE)).viewRect))
- TEClick(thePoint,(theEvent->modifiers & shiftKey) != 0,theDocument->theTE);
-
- if (gCanDrag)
- TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoBackgroundContent()
- // *
- // * Handles mouseDown events in the content region of a document window
- // * when the window is not frontmost. The following bullet items describe how this background
- // * mouseDown event is handled:
- // *
- // * (1) If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
- // * SelectWindow to bring the window to the front as usual.
- // *
- // * (2) If the mouseDown is in a draggable object and the mouse is released without
- // * dragging, call SelectWindow when the mouse is released.
- // *
- // * (3) If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
- // * should only be called if the drop occurred in the same window (the DragText function
- // * calls SelectWindow in this case).
- // *
- // *****************************************************************************
- void DoBackgroundContent(Document* theDocument, EventRecord* theEvent)
- {
- Point thePoint;
-
- SetPort((GrafPtr)theDocument->theWindow);
-
- thePoint = theEvent->where;
- GlobalToLocal(&thePoint);
-
- if (PtInRgn(thePoint,theDocument->hiliteRgn))
- {
- if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
- SelectWindow((WindowRef)theDocument->theWindow);
- else
- SelectWindow((WindowRef)theDocument->theWindow);
- }
- else
- {
- SelectWindow(theDocument->theWindow);
- DoContent(theDocument,theEvent);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoMouseDown()
- // *
- // * (1) If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
- // * SelectWindow to bring the window to the front as usual.
- // *
- // * (2) If the mouseDown is in a draggable object and the mouse is released without
- // * dragging, call SelectWindow when the mouse is released.
- // *
- // * (3) If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
- // * should only be called if the drop occurred in the same window (the DragText function
- // * calls SelectWindow in this case).
- // *
- // *****************************************************************************
- void DoMouseDown(EventRecord* theEvent)
- {
- short thePart;
- WindowPtr theWindow;
- Rect dragRect;
- Document* theDocument;
-
- thePart = FindWindow(theEvent->where,&theWindow);
- switch(thePart)
- {
- case inMenuBar:
- AdjustMenus();
- DoMenuCommand(MenuSelect(theEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(theEvent,theWindow);
- break;
-
- case inContent:
- theDocument = IsDocumentWindow(theWindow);
- if (theWindow == (WindowPtr)FrontWindow())
- DoContent(theDocument,theEvent);
- else
- DoBackgroundContent(theDocument,theEvent);
- break;
-
- case inDrag:
- if (theWindow != (WindowPtr)FrontWindow())
- SelectWindow((WindowRef)theWindow);
- dragRect = qd.screenBits.bounds;
- DragWindow((WindowRef)theWindow,theEvent->where,&dragRect);
- break;
-
- case inGrow:
- if (theDocument = IsDocumentWindow(theWindow))
- {
- GrowDocumentWindow(theWindow,theEvent->where);
- if (theDocument->theTE != NULL)
- TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (theDocument = IsDocumentWindow(theWindow))
- if ((TrackBox(theWindow,theEvent->where,thePart))&&(theDocument))
- DoZoomDocument(theDocument,theWindow,thePart);
- break;
-
- case inGoAway:
- if (theDocument = IsDocumentWindow(theWindow))
- if (TrackGoAway((WindowRef)theWindow,theEvent->where))
- {
- CloseDocument(theDocument,false);
- AdjustMenus();
- DrawMenuBar();
- }
- break;
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoKey()
- // *
- // * Called each time a character is typed on the keyboard to
- // * be entered into a document window.
- // *
- // *****************************************************************************
- void DoKey(char theChar)
- {
- WindowPtr theWindow;
- Document* theDocument;
-
- if (theWindow = (WindowPtr)FrontWindow())
- {
- if (theDocument = IsDocumentWindow(theWindow))
- {
- SetPort((GrafPtr)theDocument->theWindow);
-
- if (theDocument->theTE != NULL)
- {
- TEKey(theChar,theDocument->theTE);
- AdjustScrollBar(theDocument);
- theDocument->dirty = true;
- TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
-
- if ((theChar < 0x1C) || (theChar > 0x1F))
- DisableUndoDrag();
- }
- }
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoKeyDown()
- // *
- // *****************************************************************************
- void DoKeyDown(EventRecord* theEvent)
- {
- char theChar;
-
- theChar = theEvent->message & charCodeMask;
-
- if (theEvent->modifiers & cmdKey)
- {
- AdjustMenus();
- DoMenuCommand(MenuKey(theChar));
- }
- else
- DoKey(theChar);
- }
-
-
- // *****************************************************************************
- // *
- // * DoActivate()
- // *
- // *****************************************************************************
- void DoActivate(EventRecord* theEvent)
- {
- WindowPtr theWindow;
- Document* theDocument;
-
- if (theWindow = (WindowPtr)theEvent->message)
- if (theDocument = IsDocumentWindow(theWindow))
- DoActivateDocument(theDocument,(theEvent->modifiers & activeFlag));
- }
-
-
- // *****************************************************************************
- // *
- // * DoUpdate()
- // *
- // *****************************************************************************
- void DoUpdate(EventRecord* theEvent)
- {
- Document* theDocument;
-
- if (theDocument = IsDocumentWindow((WindowPtr)theEvent->message))
- UpdateWindow(theDocument);
- }
-
-
- // *****************************************************************************
- // *
- // * DoOSEvent()
- // *
- // *****************************************************************************
- void DoOSEvent(EventRecord* theEvent)
- {
- Document* theDocument;
-
- switch ((theEvent->message >> 24) & 0x0FF)
- {
- case suspendResumeMessage:
- gInBackground = (theEvent->message & resumeFlag) == 0;
- if (theDocument = IsDocumentWindow((WindowPtr)FrontWindow()))
- DoActivateDocument(theDocument, !gInBackground);
- break;
- }
- }
-
-
- // *****************************************************************************
- // *
- // * MyHandleOAPP()
- // *
- // *****************************************************************************
- pascal OSErr MyHandleOAPP(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
- {
- return noErr;
- }
-
-
- // *****************************************************************************
- // *
- // * MyHandleODOC()
- // *
- // *****************************************************************************
- pascal OSErr MyHandleODOC(AppleEvent* theAppleEvent, AppleEvent* /*reply*/, long /*handlerRefCon*/)
- {
- AEDescList docList;
- AEKeyword keyword;
- DescType returnedType;
- FSSpec theFSSpec;
- Size actualSize;
- long itemsInList;
- short index;
- OSErr result = noErr;
- FInfo fileInfo;
-
- if (result = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList))
- return result;
-
- if (result = AECountItems(&docList,&itemsInList))
- return result;
-
- for (index=1;index<=itemsInList;index++)
- {
- if (result = AEGetNthPtr(&docList,index,typeFSS,&keyword,&returnedType,(Ptr)&theFSSpec,sizeof(FSSpec),&actualSize))
- return result;
-
- // decide if the doc we are opening is a PICT or TEXT
- result = FSpGetFInfo(&theFSSpec,&fileInfo);
- if (result == noErr)
- {
- if (fileInfo.fdType == 'TEXT')
- DoOpenFile(&theFSSpec,false);
- else
- DoOpenFile(&theFSSpec,true);
- }
- }
- return result;
- }
-
-
- // *****************************************************************************
- // *
- // * MyHandleQUIT()
- // *
- // *****************************************************************************
- pascal OSErr MyHandleQUIT(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
- {
- Document* theDocument;
-
- gQuitting = true;
-
- while ((gQuitting) && (theDocument = IsDocumentWindow((WindowPtr)FrontWindow())))
- CloseDocument(theDocument,true);
-
- if (gQuitting)
- gQuit = true;
-
- return noErr;
- }
-
-
- // *****************************************************************************
- // *
- // * ScrollProc()
- // *
- // *****************************************************************************
- pascal void ScrollProc(ControlHandle theControl, short theCode)
- {
- short delta = 0, pageDelta;
- Document* theDocument;
- Rect viewRect;
- SInt32 theCtrlRef = GetControlReference(theControl);
-
- if (theCode == 0)
- return;
-
- theDocument = (Document*)theCtrlRef;
-
- viewRect = (**(theDocument->theTE)).viewRect;
- pageDelta = ((viewRect.bottom - viewRect.top) / ScrollResolution) - 1;
-
- switch(theCode)
- {
- case kControlUpButtonPart:
- delta = -1;
- break;
- case kControlDownButtonPart:
- delta = 1;
- break;
- case kControlPageUpPart:
- delta = -pageDelta;
- break;
- case kControlPageDownPart:
- delta = pageDelta;
- break;
- }
-
- SetControlValue(theControl,GetControlValue(theControl) + delta);
- AdjustDocumentView((Document*)theDocument);
- }
-
-
- // *****************************************************************************
- // *
- // * DoHighLevelEvent()
- // *
- // *****************************************************************************
- void DoHighLevelEvent(EventRecord* theEvent)
- {
- AEProcessAppleEvent(theEvent);
- }
-
-
- // *****************************************************************************
- // *
- // * DoEvent()
- // *
- // *****************************************************************************
- void DoEvent(EventRecord* theEvent)
- {
- WindowPtr theWindow;
- Document* theDocument;
-
- if (theWindow = (WindowPtr)FrontWindow())
- if (theDocument = IsDocumentWindow(theWindow))
- gFrontDocument = theDocument;
-
- switch(theEvent->what)
- {
- case mouseDown:
- DoMouseDown(theEvent);
- break;
- case mouseUp:
- break;
- case keyDown:
- case autoKey:
- DoKeyDown(theEvent);
- break;
- case activateEvt:
- DoActivate(theEvent);
- break;
- case updateEvt:
- DoUpdate(theEvent);
- break;
- case osEvt:
- DoOSEvent(theEvent);
- break;
- case kHighLevelEvent:
- DoHighLevelEvent(theEvent);
- break;
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoIdle()
- // *
- // *****************************************************************************
- void DoIdle()
- {
- WindowPtr theWindow;
- Document* theDocument;
-
- if (theWindow = (WindowPtr)FrontWindow())
- if (theDocument = IsDocumentWindow(theWindow))
- if (theDocument->theTE != NULL)
- {
- SetPort((GrafPtr)theDocument->theWindow);
- TEIdle(theDocument->theTE);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * EventLoop()
- // *
- // *****************************************************************************
- void EventLoop()
- {
- short gotEvent;
- EventRecord theEvent;
- RgnHandle theMouseRgn;
-
- theMouseRgn = NewRgn();
- do {
- gotEvent = WaitNextEvent(everyEvent,&theEvent,SleepDuration,theMouseRgn);
- if (gotEvent)
- {
- AdjustCursor(theEvent.where,theMouseRgn);
- DoEvent(&theEvent);
- AdjustCursor(theEvent.where,theMouseRgn);
- }
- else
- DoIdle();
- }
- while (!gQuit);
-
- DisposeRgn(theMouseRgn);
- }